package com.xnyzc.lhy.order.service.impl.order;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.approval.EApprovalMatter;
import com.xnyzc.lhy.common.penum.approval.EApprovalOrderStatus;
import com.xnyzc.lhy.common.penum.order.EOrderStatus;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.order.entity.approval.OaApprovalTempPrimary;
import com.xnyzc.lhy.order.entity.gps.OaDVehicleGps;
import com.xnyzc.lhy.order.entity.order.OaCOrderApprovalOfficial;
import com.xnyzc.lhy.order.entity.param.order.OrderParam;
import com.xnyzc.lhy.order.entity.result.user.UserLoginResult;
import com.xnyzc.lhy.order.mapper.order.OaCOrderApprovalOfficialMapper;
import com.xnyzc.lhy.order.service.impl.approval.OaApprovalTaskServiceImpl;
import com.xnyzc.lhy.order.service.order.IOaCOrderApprovalOfficialService;
import com.xnyzc.lhy.order.service.order.IOaDOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 审批公务订单 服务实现类
 * </p>
 *
 * @author lhy@xnyzc.com
 * @since 2019-07-22
 */
@Service
public class OaCOrderApprovalOfficialServiceImpl extends ServiceImpl<OaCOrderApprovalOfficialMapper, OaCOrderApprovalOfficial> implements IOaCOrderApprovalOfficialService {

    @Autowired
    private IOaDOrderService oaDOrderService;

    @Autowired
    OaApprovalTaskServiceImpl oaApprovalTaskServiceImpl;

    /**
     * @param approvalOfficial
     * @param oaDOrder
     * @param vehicleGps
     * @return
     */
    @Override
    public boolean distributeOrderApprovalOfficial(OaCOrderApprovalOfficial approvalOfficial, OrderParam oaDOrder, OaDVehicleGps vehicleGps) {
        try {
            oaDOrder.setOrderNo(approvalOfficial.getOrderNo());
            oaDOrder.setUserOrderId(String.valueOf(approvalOfficial.getApprovalOfficialId()));
            oaDOrder.setTag("lhy");
            oaDOrderService.addOrder(vehicleGps, oaDOrder);
            return this.updateById(approvalOfficial);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 审批公务提交申请单
     *
     * @param approvalOfficial
     * @return
     */
    @Override
    public String orderApprovalOfficial(OaCOrderApprovalOfficial approvalOfficial, OrderParam orderParam, UserLoginResult userLoginResult) {
        long id = IDUtil.nextId();
        // 初始化订单
        approvalOfficial.setApprovalOfficialId(id);
        approvalOfficial.setOfficeId(userLoginResult.getOfficeId());
        approvalOfficial.setOrderNo(orderParam.getOrderNo());

        // 初始化审批流ID
        OaApprovalTempPrimary getApprovalTempInfo = oaApprovalTaskServiceImpl.getApprovalTempInfo(userLoginResult.getOfficeId(), EApprovalMatter.takeATaxi.getValue());
        if (CheckUtil.objIsEmpty(getApprovalTempInfo)) {
            throw new PangException(EErrorCode.noApproval);
        }
        approvalOfficial.setApprovalId(getApprovalTempInfo.getApprovalId());
        approvalOfficial.setApprovalStatus(EApprovalOrderStatus.approvaling.getValue());
        approvalOfficial.setApplyMatter(EApprovalMatter.takeATaxi.getValue());
        approvalOfficial.setApprovalNode(1);
        approvalOfficial.setTag(TokenUtil.getTag());
        approvalOfficial.setApplyReason(orderParam.getApplyReason());
        approvalOfficial.setOrderStatus(EOrderStatus.examination.getType());
        if (!this.save(approvalOfficial)) {
            throw PangException.create(EErrorCode.noData);
        }
        return String.valueOf(id);
    }
}
